<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

class f_mail_MailMessage
{
	protected $sender = null;
	protected $senderName = null;
	protected $replyTo = null;
	protected $bcc = null;
	protected $cc = null;
	protected $subject = null;
	protected $receiver = null;
	protected $receiverName = null;
	protected $encoding = 'utf-8';
	protected $html = null;
	protected $text = null;
	protected $moduleName = null;
	protected $attachment = array();

	/**
	 * Enter description here...
	 *
	 * @return f_mail_MailMessage
	 */
	public static function getInstance()
	{
		$className = get_class();
		return new $className();
	}

	/**
	 * @param string $sender
	 * @return f_mail_MailMessage
	 */
	public function setSender($sender)
	{
		$this->sender = $sender;
		return $this;
	}
	/**
	 * @param string $sender
	 * @return f_mail_MailMessage
	 */
	public function setSenderName($senderName)
	{
		$this->senderName = $senderName;
		return $this;
	}

	/**
	 * @return String
	 */
	public function getSender()
	{
		return $this->sender;
	}
	
	/**
	 * @return String
	 */
	public function getSenderName()
	{
		return $this->senderName;
	}

	/**
	 * @param string $replyTo
	 * @return f_mail_MailMessage
	 */
	public function setReplyTo($replyTo)
	{
		$this->replyTo = $replyTo;
		return $this;
	}

	/**
	 * @return String
	 */
	public function getReplyTo()
	{
		return $this->replyTo;
	}

	/**
	 * @param string $bcc
	 * @return f_mail_MailMessage
	 */
	public function setBcc($bcc)
	{
		$this->bcc = $bcc;
		return $this;
	}

	/**
	 * @return String
	 */
	public function getBcc()
	{
		return $this->bcc;
	}

	/**
	 * @param string $cc
	 * @return f_mail_MailMessage
	 */
	public function setCc($cc)
	{
		$this->cc = $cc;
		return $this;
	}

	/**
	 * @return String
	 */
	public function getCc()
	{
		return $this->cc;
	}

	/**
	 * @param string $subject
	 * @return f_mail_MailMessage
	 */
	public function setSubject($subject)
	{
		$this->subject = $subject;
		return $this;
	}

	/**
	 * @return String
	 */
	public function getSubject()
	{
		return $this->subject;
	}

	/**
	 * @param string $receiver
	 * @return f_mail_MailMessage
	 */
	public function setReceiver($receiver)
	{
		$this->receiver = $receiver;
		return $this;
	}

	/**
	 * @return String
	 */
	public function getReceiver()
	{
		return $this->receiver;
	}

	/**
	 * @param string $receiverName
	 * @return f_mail_MailMessage
	 */
	public function setReceiverName($receiverName)
	{
		$this->receiverName = $receiverName;
		return $this;
	}

	/**
	 * @return String
	 */
	public function getReceiverName()
	{
		return $this->receiverName;
	}
	
	/**
	 * @param string $attachment
	 * @return f_mail_MailMessage
	 */
	public function addAttachment($attachment)
	{
		$this->attachment[] = $attachment;
		return $this;
	}

	/**
	 * @return array
	 */
	public function getAttachment()
	{
		return $this->attachment;
	}

	/**
	 * @param string $encoding
	 * @return f_mail_MailMessage
	 */
	public function setEncoding($encoding)
	{
		$this->encoding = $encoding;
		return $this;
	}

	/**
	 * @return String
	 */
	public function getEncoding()
	{
		return $this->encoding;
	}

	/**
	 * @param string $htmlBody
	 * @param string $textBody
	 * @return f_mail_MailMessage
	 */
	public function setHtmlAndTextBody($htmlBody, $textBody = null)
	{
		$this->html = $htmlBody;
		$this->text = $textBody;
		return $this;
	}

	/**
	 * @return String
	 */
	public function getHtmlContent()
	{
		return $this->html;
	}

	/**
	 * @return String
	 */
	public function getTextContent()
	{
		return $this->text;
	}

    public function setContentFromTemplate($moduleName, $template, $var = array())
    {
        $filePathInModule = "templates".DS."mail".DS.$template;
        $templatePath = f_file_Resolver::getModuleFilePath($moduleName, $filePathInModule);

        //Send content to the template
        if(!is_null($templatePath) && is_array($var))
        {
            $template = f_template_Template::getInstance()->disableCache();
            $template->setTemplate($templatePath);
            foreach($var as $varName => $value)
            {
                $template->assign($varName, $value);
            }
            $this->setHtmlAndTextBody($template->render());
        }

        return $this;
    }
}